﻿using System.Collections.Generic;
using System.Linq;
using TimbreRecognition.Recognition.Model;
using TimbreRecognition.Recognition.NEAT;
using TimbreRecognition.Recognition.Teacher;

namespace TimbreRecognition.Recognition
{
    public class NeuralNetworkBuilder
    {
        private const double Error = 0.01;

        private static readonly int[] HiddenLayers = { 28 };

        public ILogger Logger { get; set; }

        public List<DataItem> TrainingData { get; set; }

        public List<DataItem> ValidationData { get; set; }


        public INetwork Build()
        {
            int numberOfInputs = TrainingData[0].InputCount;

            int numberOfOutputs = TrainingData[0].ExpectedOutput.Length;

            NeuronNetwork network = NeuronNetworkFactory.create(numberOfInputs, numberOfOutputs, HiddenLayers);
            BackPropagationTeacher teacher = new BackPropagationTeacher(Error) { Logger = Logger };
            Dictionary<double[], double[]> trainingData = 
                TrainingData.ToDictionary(o => o.DataSeries, o => o.ExpectedOutput);
            Dictionary<double[], double[]> validationData = 
                ValidationData.ToDictionary(o => o.DataSeries, o => o.ExpectedOutput);
            teacher.teach(network, trainingData, validationData);

            NEATNetworkCreator neatCreator = new NEATNetworkCreator { Logger = Logger };
           // INetwork neatNetwork = neatCreator.Create(NumberOfInputs, NumberOfOutputs, trainingData, validationData);

            return network;
        }

      
    }
}
